package com.weishenai.customerassistant.controller.weChat;

import com.weishenai.customerassistant.common.Result;
import com.weishenai.customerassistant.service.QrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Api(tags = "小程序_文件操作")
@Slf4j
@RestController
@RequestMapping("/wechat/file")
public class FileWechatController {

    @Value("${file.imageSavePath}")
    private String fileSavePath;

    @Value("${file.staticExcelDir}")
    private String staticExelDir;
    /**
     * 时间格式化
     */
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 格式化时间中间不能有空格

    @ApiOperation("小程序 上传图片返回URL")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片"),
            @ApiImplicitParam(name = "id", value = "工单编号")
    })
    @PostMapping(value = "/upload/{id}")
    public Object uploadFile(MultipartFile file, HttpServletRequest request, @PathVariable("id") Long id) {
        if (file == null) return Result.failed("上传文件为空...");
        String directory = format.format(new Date());
        File dir = new File(fileSavePath + directory);
        if (!dir.exists()) {
            log.info("创建一个新的目录：" + directory);
            dir.mkdirs();
        }
        //给文件重新设置一个名字
        //后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String newFileName = "image" + id + suffix;
        log.info("上传图片文件名：" + newFileName);
        //4.创建这个新文件
        File newFile = new File(fileSavePath + directory + "/" + newFileName);
        if (newFile.exists()) {
            log.info("文件已经存在，覆盖原来的文件，文件名:" + newFile.getName());
            newFile.delete();
        }
        //5.复制操作
        try {
            file.transferTo(newFile);
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/image/" + directory + "/" + newFileName;
            log.info("图片上传，访问URL：" + url);
            return Result.success(url);
        } catch (IOException e) {
            return Result.failed("失败");
        }
    }

    @ApiOperation("查询所有Excel")
    @GetMapping("/getAllExcelFile")
    private Object getAllExcelFile() {
        File file = new File(staticExelDir);
        File[] files = file.listFiles();
        if (files.length == 0) return Result.failed("失败");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            list.add(files[i].getName());
        }
        return Result.success(list);
    }


}
